package com.tgpms.web.assets.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.web.assets.entity.MaterialReceivingReport;
import com.tgpms.web.assets.service.MaterialReceivingReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MaterialReceivingReportServiceImpl
 * @Description TODO
 * @Author GUAN
 * @Date 2019-11-12 17:24
 * @Version 1.0
 * @return
 **/
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class MaterialReceivingReportServiceImpl implements MaterialReceivingReportService {

    @Autowired
    private VformInterface vformInterface;

    @Override
    public Result selMaterialReceivingReportList(PageInfo pageInfo) {
        JSONObject param = new JSONObject();
        Object selparam = pageInfo.getQueryMap().get("selparam");
        Object leftdition = pageInfo.getQueryMap().get("leftdition");
        Object condition = pageInfo.getQueryMap().get("condition");
        Object dataowner = pageInfo.getQueryMap().get("dataowner");
        if(Common.isEmpty(selparam)) {
            param.put("[SQL1]", " ");
        }else {
            param.put("[SQL1]", selparam.toString());
        }
        if(Common.isEmpty(leftdition)) {
            param.put("[SQL2]", " ");
        }else {
            param.put("[SQL2]", leftdition.toString());
        }
        if(Common.isEmpty(condition)) {
            param.put("[SQL3]", " ");
        }else {
            param.put("[SQL3]", condition.toString());
        }
        Result result = vformInterface.multiForMsQueryBySql("SCWZC030", param.toJSONString(), pageInfo.getPage(),
                pageInfo.getSize(), "Y", "Y", dataowner.toString());
        return result;
    }

    @Override
    public Map<String, String> addMaterialReceivingReportInfo(MaterialReceivingReport materialReceivingReport) {
        Map<String,String> map = new HashMap<String, String>();
        //对象转json字符串, 字段驼峰转下划线大写字段
        JSONObject json = VformBeanUtil.entityToJsonString(materialReceivingReport);
        if(json==null || json.isEmpty()) {
            map.put("code", "-2");
            map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
            return map;
        }
        System.out.println("vform增加上送参数   "+json.toString());
        //vform同步插入
        Result result = vformInterface.insert("D8025", json);
        System.out.println("vform增加返回结果   "+result);
        if(result==null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        if(resData.equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        }
        else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
            map.put("data", materialReceivingReport.getMrr());
        }
        return map;
    }

    @Override
    public Map<String, String> updateMaterialReceivingReportInfo(MaterialReceivingReport materialReceivingReport) {
        Map<String,String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("PROJECT", materialReceivingReport.getProject());
        keyjson.put("MRR", materialReceivingReport.getMrr());
        JSONObject pajson = new JSONObject();
        pajson = VformBeanUtil.entityToJsonStringByUpdate(materialReceivingReport);
        pajson.remove("ISSUE");

        System.out.println("vform更新上送主键   "+keyjson.toString());
        System.out.println("vform更新上送参数   "+pajson.toString());
        //vform同步更新
        Result result = vformInterface.update("D8025", keyjson.toJSONString(),pajson.toJSONString(),"Y","Y",materialReceivingReport.getDataOwner());
        System.out.println("vform更新返回结果   "+result);
        if(result==null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        }else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
        }
        return map;
    }

    @Override
    public Map<String, String> deleteMaterialReceivingReportInfo(String project, String mrr) {
        Map<String,String> map = new HashMap<String, String>();
        JSONObject keyjson = new JSONObject();
        keyjson.put("PROJECT", project);
        keyjson.put("MRR", mrr);
        System.out.println("vform删除上送主键   "+keyjson.toString());
        //vform同步删除
        Result result = vformInterface.delete("D8025", keyjson.toJSONString());
        System.out.println("vform删除返回结果   "+result);
        if(result==null || !result.getSuccess()) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
            return map;
        }
        if(result.getData().toString().equals("{\"message\":\"login\"}")) {
            map.put("code", "-3");
            map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
            return map;
        }
        String resData = result.getData().toString();
        JSONObject reJson = JSONObject.parseObject(resData);
        if(reJson.get("status").toString().equals("error")) {
            map.put("code", "-4");
            map.put("info", reJson.get("validateError").toString());
            return map;
        }else {
            map.put("code", "1");
            map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
        }
        return map;
    }
}
